define(['zepto', 'sui', 'utils', 'wxshare', 'submitForm'], function($, utils, wxshare) {
  return {
    init: function(data) {
      this.roundId = data.roundId;
      this.batchId = data.batchId;
      this.week = data.week;
      this.breakfasts = data.breakfasts;
      this.lunchs = data.lunchs;
      this.dinners = data.dinners;
      this.snacks = data.snacks;
      
      this.initClick();
      
      this.initPicker();
      
      this.initSubmit();
    },
    
    initPicker: function() {
        var that = this;
        $(".diet-modify, .set-meal").each(function(e){
            
            var type = $(this).data("type");
            var diets = that.initPickerData(type);
            
            var dietDisplay = new Array(),
                dietValue = new Array();
            $.each(diets, function(k, diet) {
                dietDisplay[k] = diet.name;
                dietValue[k] = diet.id;
            })
            
          $(this).picker({
                toolbarTemplate: '<header class="bar bar-nav">\
                    <button class="button button-link pull-right close-picker">确定</button>\
                    <h1 class="title">饮食</h1>\
                    </header>',
                    cols: [
                      {
                        textAlign: 'center',
                        values: dietValue,
                        displayValues: dietDisplay
                      }
                    ],
                    formatValue: function(picker, value, displayValue) {
                       that.value = value[0];
                       return displayValue
                    },
                    onClose: function(e) {
                      var $clicker = e.input
                      
                      if($($clicker).hasClass("set-meal")) {
                    	  $(e.input).parent().parent().find(".action-header").find("small").html(e.displayValue.toString());
                    	  $(e.input).parent().parent().find(".action-header").find("input[type='hidden']").val(e.value);
                    	  $(e.input).parent().parent().find(".action-header").find("a").css('display','block');
                    	  $(e.input).css('display','none');
                    	  $(e.input).parent().find('.intro').css('display','block');
                    	  $.ajax({
                              url: window.__CTX__.root + '/rounds/dietary-template/' + e.value + '/description',
                              type: 'POST',
                              success: function(res){
                                  var res = JSON.parse(res);
                                  if(res.code == 0) {
                                  	$(e.input).parent().find('textarea').val(res.data.description);
                                  	
                                  } else {
                                  	$.toast("请求失败")
                                  } 
                              },
                              fail: function() {
                                  $.toast("请求失败")
                              }
                          })
                      } else {
                    	  var displayVal = e.displayValue;
                          $(e.input).parents(".action-header").find("small").html(displayVal.toString())
                          $(e.input).parent().find('input[type="hidden"]').val(e.value);
                          $.ajax({
                              url: window.__CTX__.root + '/rounds/dietary-template/' + e.value + '/description',
                              type: 'POST',
                              success: function(res){
                                  var res = JSON.parse(res);
                                  if(res.code == 0) {
                                  	$(e.input).parent().parent().find('textarea').val(res.data.description);
                                  } else {
                                  	$.toast("请求失败")
                                  } 
                              },
                              fail: function() {
                                  $.toast("请求失败")
                              }
                          })
                      }
                      
                    }
                });
        })
    },
    
    initPickerData: function(type) {
        var that = this;
        var diets;
        if(type === 'breakfast') {
            diets = that.breakfasts;
        } else if(type === 'lunch') {
            diets = that.lunchs;
        } else if(type === 'dinner') {
            diets = that.dinners;
        } else if(type === 'snack') {
            diets = that.snacks;
        }
        
        return diets;
    },
    
    
  createDiet: function(url, type, clicker) {
    if(url === 'snacks') {
        var meal = $('textarea[name="' + type + 'Description"]').val().trim();
        if(!meal) {
            $.toast("请先添加正餐！");
            return false;
        }
    }
  },
  
    initClick: function() {
        
        var that = this;
        
        $("textarea").on('change', function() {
            $(".save-diet").css("display", "block");
        })
        
        
        
        $(document).on('click', '.save-diet', function() {
        
            var breakfastDescription = $("textarea[name='breakfastDescription']").val().trim();
            var lunchDescription = $("textarea[name='lunchDescription']").val().trim();
            var dinnerDescription = $("textarea[name='dinnerDescription']").val().trim();
            var breakfastSnacksDescription = $("textarea[name='breakfastSnacksDescription']").val().trim();
            var lunchSnacksDescription = $("textarea[name='lunchSnacksDescription']").val().trim();
            var dinnerSnacksDescription = $("textarea[name='dinnerSnacksDescription']").val().trim();
            
            if(!breakfastDescription || !lunchDescription || !dinnerDescription) {
              $.toast("早中晚饮食不能为空");
              return false;
            }
            
            var data = {
             'breakfastDescription':breakfastDescription,
             'lunchDescription':lunchDescription,
             'dinnerDescription':dinnerDescription,
             'breakfastSnacksDescription':breakfastSnacksDescription,
             'lunchSnacksDescription':lunchSnacksDescription,
             'dinnerSnacksDescription':dinnerSnacksDescription,
             '_method': "PATCH"
            }
            
            $.ajax({
                url: window.__CTX__.root + '/rounds/' + that.roundId + '/batch-coursewares/' + that.batchId,
                type: 'POST',
                data: data,
                success: function(res){
                    var res = JSON.parse(res);
                    if(res.code == 0) {
                        $.toast("修改成功");
                        setTimeout(function(e){
                            window.location.reload();
                        }, 1000)
                    }   
                },
                fail: function() {
                    $.toast("请求失败")
                }
            })
        })
        
        
    },
    
    initSubmit: function() {
      $(".button-save").on("click", function() {
          var breakfastDescription = $("textarea[name='breakfastDescription']").val().trim();
          var lunchDescription = $("textarea[name='lunchDescription']").val().trim();
          var dinnerDescription = $("textarea[name='dinnerDescription']").val().trim();
          
          if(!breakfastDescription || !lunchDescription || !dinnerDescription) {
            $.toast("早中晚饮食不能为空");
            return false;
          }
          
        $('button[type="submit"]').trigger("click")
      })
      $("form").submitform({
        successUrl: $(".fs-header-small a").attr("href")
      });
    }
    
  }
})